/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.GroupDirectoryRelationship;

/**
 * An instance of GroupDirectoryRelationshipDAO is a data access object used to
 * access GroupDirectoryRelationship, which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class GroupDirectoryRelationshipDAO extends BaseDAO {

	public GroupDirectoryRelationship findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		GroupDirectoryRelationship instance = (GroupDirectoryRelationship) session
				.get(GroupDirectoryRelationship.class.getName(), id);

		return instance;

	}

	public List<GroupDirectoryRelationship> findByGidAndDid(
			java.lang.Integer gid, Integer did) {
		Session session = HibernateSessionFactory.getSession();

		String queryString = "from GroupDirectoryRelationship where gid=? and did=?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, did);
		@SuppressWarnings("unchecked")
		List<GroupDirectoryRelationship> results = queryObject.list();

		return results;

	}

	public List<GroupDirectoryRelationship> findByProperty(String propertyName,
			Object value, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupDirectoryRelationship as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);

		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<GroupDirectoryRelationship> results = queryObject.list();

		return results;

	}

	public void deleteByProperty(String propertyName, Object value,
			Session session) {

		String queryString = "from GroupDirectoryRelationship as model where model."
				+ propertyName + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<GroupDirectoryRelationship> results = queryObject.list();

		for (Iterator<GroupDirectoryRelationship> iter = results.iterator(); iter
				.hasNext();) {
			GroupDirectoryRelationship persistentInstance = iter.next();
			session.delete(persistentInstance);
		}

	}

	public List<GroupDirectoryRelationship> findByProperty(String propertyName,
			Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupDirectoryRelationship as model where model."
				+ propertyName + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<GroupDirectoryRelationship> results = queryObject.list();

		return results;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from GroupDirectoryRelationship as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<GroupDirectoryRelationship> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupDirectoryRelationship";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<GroupDirectoryRelationship> results = queryObject.list();

		return results;

	}

}